package com.peng.leetcode.sort;

/**
 * ContainsNearbyAlmostDuplicate
 *
 * @author: lupeng6
 * @create: 2020/12/11 15:10
 */
public class ContainsNearbyAlmostDuplicate {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 1};
        int k = 3;
        int t = 0;

//        int[] arr = {-2147483648, 2147483647};
//        int k = 1;
//        int t = 1;
//        int[] arr = {2147483646, 2147483647};
//        int k = 3;
//        int t = 3;
        System.out.println(new Solution().containsNearbyAlmostDuplicate(arr, k, t));
    }

    static class Solution {
        public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
            for (int i = 0; i < nums.length; i++) {
                int maxEndIndex = Math.min(i + k + 1, nums.length);
                for (int j = i + 1; j < maxEndIndex; j++) {
                    if (Math.abs(Long.valueOf(nums[i]) - Long.valueOf(nums[j])) <= t && Math.abs(i - j) <= k) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
